home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / ai / prlg195b.lzh / SAMPLES.LZH / LISTS.PRO < prev    next >
Text File  |  1987-11-11  |  4KB  |  131 lines

  1. /* ------------------------------------------------------------------
  2.  
  3.     LISTS.PRO    by  Eugene L. Frassetto
  4.  
  5.    ------------------------------------------------------------------
  6.  
  7.     This file contains a collection of list manipulation
  8.     predicates.  While you may find theme useful in your
  9.     programs, they are most usefull in demonstrating the
  10.     recursive nature of prolog and a general way in which
  11.     lists may be manipulated in a prolog program.
  12.  
  13.     -----------------------------------------------------
  14.  
  15.     predicates in the file:
  16.  
  17.     is_list         last_of( list, X )
  18.     length_of        append( list1, list2, X )
  19.     head_of         member_of ( list, X )
  20.     second_of        reverse( list, X )
  21.     tail_of         sum_of( list, X )
  22.     nth_of            dif_of( list, X )
  23.  
  24. ----------------------------------------------------------------------- */
  25.  
  26.  
  27.  
  28.  
  29. /* -----------------------------------------------
  30.    test if a variable is a list
  31.    is_list( [list] ). => yes or no.
  32. -------------------------------------------------- */
  33. is_list( [] ).
  34. is_list( [Head | Tail] ) :- is_list( Tail ).
  35.  
  36.  
  37. /* ----------------------------------
  38. /* get the length of a list
  39. ------------------------------------- */
  40. length_of( [], 0 ).
  41. length_of( [_ | Tail], Length ) :-
  42.     NewLen is Length,
  43.     length_of( Tail, NewLen ), Length is NewLen + 1.
  44.  
  45.  
  46. /* -------------------------------------------------------
  47.    get the head or second element or the tail of a list
  48.    head_of( [list], X ). => X = list-head.
  49.    second_of( [list], X ). => X = list-element-2.
  50.    tail_of( [list], X ). => X = list-tail.
  51. ---------------------------------------------------------- */
  52. head_of( [Head | Tail], Head).
  53. second_of( [_, Second | Tail], Second ).
  54. tail_of( [Head | Tail], Tail).
  55.  
  56.  
  57. /* ------------------------------------------------------
  58.    get the nth element of a list  (0 based)
  59.    nth_of( [], anynum, X ). => [].
  60.    nth_of( [a,b,c], 1, X ). => b.
  61.    nth_of( [a,b,c], 4, X ). => [].
  62. ---------------------------------------------------------- */
  63. nth_of( [], N, [] ).
  64. nth_of( [H | T], 0, H ).
  65. nth_of( [H | T], N, X ) :- C is N - 1, nth_of( T, C, X).
  66.  
  67.  
  68. /* --------------------------------------------------
  69.    get the last element of a list
  70.    last_of( [list], X ). => X = last-element.
  71. ------------------------------------------------------- */
  72. last_of( [X], X ).
  73. last_of( [_ | T], X) :- last_of( T, X).
  74.  
  75.  
  76. /* --------------------------------------------------------
  77.    append to a list
  78.    append( [list-1], [list-2], X). => X = [list-1, list-2].
  79.    NOTE: entering append(X,[d,e],[a,b,c,d,e]) => X = [a,b,c].
  80.           append([a,b],X,[a,b,c,d,e]) => X = [c,d,e].
  81. ---------------------------------------------------------- */
  82. append( [], AnyList, AnyList).
  83. append( [Head | Tail], List2, [Head | NewTail] ) :-
  84.     append( Tail, List2, NewTail ).
  85.  
  86. /* --------------------------------------------------------
  87.    check if an element of a list
  88.    member_of( [list], what ). => yes or no.
  89.    member_of( [list], X ). => X = first, X = second, . . .
  90. ----------------------------------------------------------- */
  91. member_of( [Element | _], Element ).
  92. member_of( [_ | Tail], Element ) :- member_of( Tail, Element ).
  93.  
  94.  
  95. /* -------------------------------------------------
  96.    reverse the order of the elements of a list
  97.    reverse([a,b,c,d],X). => X = [d,c,b,a].
  98.    reverse([a,b,c],[c,b,a]). => Yes.
  99. ---------------------------------------------------- */
  100. reverse([F], [F]).
  101. reverse([H | T], X) :-
  102.     reverse(T, Y),
  103.     append(Y, [H], X).
  104.  
  105.  
  106. /* ------------------------------------------------
  107.    add a list of numbers
  108.    sum_of([2],X). => X = 2.
  109.    sum_of([1,2,3,4,5],X). => X = 15.
  110.    sum_of([1,2,3],6). => Yes.
  111.    sum_of([1,2,3],10). => No.
  112. ---------------------------------------------------- */
  113. sum_of( [X], X).
  114. sum_of( [F, S | T], X) :-
  115.     R is F + S,
  116.     sum_of( [R | T], X).
  117.  
  118.  
  119. /* ------------------------------------------------
  120.    subtract a list of numbers
  121.    dif_of([2],X). => X = 2.
  122.    dif_of([10,5],X). => X = 5.
  123.    dif_of([1,2,3,4,5],X). => X = -13.
  124.    dif_of([1,2,3],-4). => Yes.
  125.    dif_of([1,2,3],-10). => No.
  126. ---------------------------------------------------- */
  127. dif_of( [X], X).
  128. dif_of( [F, S | T], X) :-
  129.     R is F - S,
  130.     dif_of( [R | T], X).
  131.